package com.ebt.m.commons.model.http;


import com.ebt.m.commons.BaseApplication;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;


import java.util.concurrent.TimeUnit;

import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by Andy.Wang on 2016/3/9.
 */
public class APIsFactory {

    private Gson gson;
    private static final int TIMEOUT_READ = 20;
    private static final int TIMEOUT_CONNECTION = 10;
    private static HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BODY);
    private CacheInterceptor cacheInterceptor = new CacheInterceptor();
    private AuthorizationInterceptor authorizationInterceptor = new AuthorizationInterceptor();

    public static <T> T createApiWithoutAuth(Class<T> clazz, String url) {
        Retrofit.Builder builder = new Retrofit.Builder()
                .baseUrl(url)
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .addConverterFactory(GsonConverterFactory.create());
        if (BaseApplication.isDebug) {
            OkHttpClient okHttpLogClient = new OkHttpClient.Builder()
                    .addInterceptor(interceptor)
                    .build();
            builder.client(okHttpLogClient);
        }
        Retrofit retrofit = builder
                .build();
        return retrofit.create(clazz);
    }

    public  <T> T createService(Class<T> clazz,String url){
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(url)
                .client(getOkHttpClient())
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .addConverterFactory(GsonConverterFactory.create(gson))
                .build();
        return retrofit.create(clazz);
    }


    public static <T> T createOldApi(Class<T> clazz, String url) {
        return createApiWithoutAuth(clazz, url);
    }

    private static class SingletonHolder {
        private static final APIsFactory INSTANCE = new APIsFactory();
    }

    public static APIsFactory getInstance() {
        return SingletonHolder.INSTANCE;
    }

    private APIsFactory() {
         gson = new GsonBuilder()
                .registerTypeAdapterFactory(new CustomTypeApdaterFactory())
                .create();
    }


    private OkHttpClient getOkHttpClient() {
        OkHttpClient.Builder builder = new OkHttpClient.Builder()
                .hostnameVerifier(org.apache.http.conn.ssl.SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER)
                .addNetworkInterceptor(cacheInterceptor)
                .cache(HttpCache.getCache())
                .addInterceptor(cacheInterceptor)
                .addInterceptor(authorizationInterceptor)
                .connectTimeout(TIMEOUT_CONNECTION, TimeUnit.SECONDS)
                .readTimeout(TIMEOUT_READ, TimeUnit.SECONDS)
                .writeTimeout(TIMEOUT_READ, TimeUnit.SECONDS)
                .retryOnConnectionFailure(true);
        if (BaseApplication.isDebug)
            builder.addInterceptor(interceptor);
        OkHttpClient okHttpClient = builder.build();
        return okHttpClient;
    }
}
